home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / cli / MCommands_1_2.lha / Src / time.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  3KB  |  107 lines

  1. /****************************************************************************/
  2. /*                                  Time.c                                  */
  3. /*                     Measure command execution time                       */
  4. /*                    Copyright © 1994 Michael Letowski                     */
  5. /****************************************************************************/
  6.  
  7. #define __USE_SYSBASE
  8.  
  9. #include <exec/types.h>
  10. #include <dos/dos.h>
  11. #include <dos/rdargs.h>
  12. #include <dos/dostags.h>
  13. #include <devices/timer.h>
  14. #include <utility/tagitem.h>
  15. #include <support/types.h>
  16. #include <support/dos.h>
  17.  
  18. #include <string.h>
  19.  
  20. #include <proto/exec.h>
  21. #include <proto/dos.h>
  22. #include <proto/timer.h>
  23.  
  24. #include "time.rev.h"
  25.  
  26. #define DOS_NAME        "dos.library"
  27. #define DOS_VERN        37
  28. #define TIMER_NAME    "timer.device"
  29.  
  30. STATIC CONST TEXT VersionString[]=
  31.     VERSION(PROG_NAME,PROG_VERSION,PROG_REVISION,PROG_DATE);
  32.  
  33. #define TEMPLATE        "NOHEADER/S,COMMAND/F/A"
  34.  
  35. struct Options
  36. {
  37.     LONG opt_NoHeader;
  38.     STRPTR opt_Command;
  39. };
  40.  
  41. ULONG Time(VOID)
  42. {
  43.     struct ExecBase *SysBase=*((struct ExecBase **)4);
  44.     struct DosLibrary *DOSBase;
  45.     struct Library *TimerBase;
  46.  
  47.     struct RDArgs *Args;
  48.     struct MsgPort *Port;
  49.     struct timerequest *TR;
  50.     struct Options Opts;
  51.     struct timeval TV1,TV2;
  52.     ULONG RC=RETURN_FAIL;
  53.  
  54.     /* Open libraries */
  55.     unless(DOSBase=(struct DosLibrary *)OpenLibrary(DOS_NAME,DOS_VERN))
  56.     {
  57.         SetResult2(ERROR_INVALID_RESIDENT_LIBRARY);
  58.         raise(NO_DOS);
  59.     }
  60.  
  61.     /* Read args */
  62.     clear(&Opts);
  63.     unless(Args=ReadArgs(TEMPLATE,(LONG *)&Opts,NULL))
  64.     {
  65.         PrintFault(IoErr(),PROG_NAME);                        /* Inform user */
  66.         raise(NO_ARGS);
  67.     }
  68.  
  69.     /* Do timing */
  70.     if(Port=CreateMsgPort())
  71.     {
  72.         if(TR=CreateIORequest(Port,sizeof(struct timerequest)))
  73.         {
  74.             if(OpenDevice(TIMER_NAME,UNIT_VBLANK,(struct IORequest *)TR,0)==0)
  75.             {
  76.                 TimerBase=(struct Library *)TR->tr_node.io_Device;
  77.  
  78.                 GetSysTime(&TV1);
  79.                 RC=SystemTags(Opts.opt_Command,SYS_UserShell,TRUE,TAG_DONE) ? 
  80.                         RETURN_ERROR : RETURN_OK;
  81.                 GetSysTime(&TV2);
  82.                 SubTime(&TV2,&TV1);
  83.  
  84.                 /* Print results */
  85.                 if(RC==RETURN_ERROR)                                        /* Error during execution */
  86.                     PrintFault(IoErr(),PROG_NAME);
  87.                 if(Opts.opt_NoHeader)
  88.                     VPrintf("%lu.%06lu\n",&TV2);
  89.                 else
  90.                     VPrintf("Execution time: %lu.%06lu s.\n",&TV2);
  91.  
  92.                 CloseDevice((struct IORequest *)TR);
  93.             }
  94.             DeleteIORequest(TR);
  95.         }
  96.         DeleteMsgPort(Port);
  97.     }
  98.  
  99.     if(RC==RETURN_FAIL)                                                /* Couldn't use timer */
  100.         CauseIoErr(ERROR_NO_FREE_STORE,PROG_NAME);
  101.  
  102.     except(NO_ARGS,FreeArgs(Args));
  103.     except(NO_DOS,CloseLibrary((struct Library *)DOSBase));
  104.  
  105.     return(RC);
  106. }    /* Time */
  107.